﻿namespace H2.Streams.Sections
{
    using H2.DataTypes;
    using H2.Streams;
    using System;
    using System.Collections.Generic;
    using System.Reflection;

    public class StringIDs
    {
        private MapStream Map;
        private List<StringID> StringIDCache;

        internal StringIDs(MapStream map)
        {
            this.Map = map;
            this.CreateCache();
        }

        public void Add(StringID stringid)
        {
            throw new Exception();
        }

        private void CreateCache()
        {
            long position = this.Map.Position;
            this.Map.Position = this.Map.Header.StringIDs128Offset;
            this.StringIDCache = new List<StringID>(this.Map.Header.StringIDCount);
            for (int i = 0; i < this.StringIDCache.Capacity; i++)
            {
                this.StringIDCache.Add(new StringID(new string(this.Map.ReadChars(0x80))));
            }
            this.Map.Position = position;
        }

        public ushort IndexOf(StringID stringid)
        {
            string str = stringid.ToString();
            for (ushort i = 0; i < this.StringIDCache.Count; i = (ushort) (i + 1))
            {
                if (this.StringIDCache[i].ToString() == str)
                {
                    return i;
                }
            }
            throw new Exception("Not In StringID Table");
        }

        public ushort IndexOf(string name)
        {
            for (ushort i = 0; i < this.StringIDCache.Count; i = (ushort) (i + 1))
            {
                if (this.StringIDCache[i].ToString() == name)
                {
                    return i;
                }
            }
            throw new Exception("Not In StringID Table");
        }

        public string[] ToStringArray()
        {
            string[] strArray = new string[this.StringIDCache.Count];
            for (int i = 0; i < this.StringIDCache.Count; i++)
            {
                strArray[i] = this.StringIDCache[i].ToString();
            }
            return strArray;
        }

        public void WriteStringIDs()
        {
            long position = this.Map.Position;
            this.Map.Position = this.Map.Header.StringIDs128Offset;
            for (int i = 0; i < this.StringIDCache.Capacity; i++)
            {
                this.Map.Write(this.StringIDCache[i].ToCharArray());
            }
            this.Map.Position = position;
        }

        public StringID this[ushort index]
        {
            get
            {
                if (index > this.StringIDCache.Count)
                {
                    char[] array = "NOT IN STRINGID TABLE".ToCharArray();
                    Array.Resize<char>(ref array, 0x80);
                    StringID gid = new StringID(new string(array));
                    gid.NOTINSTRINGIDTABLE = true;
                    return gid;
                }
                return this.StringIDCache[index];
            }
            set
            {
                this.Map.WriteAt(this.Map.Header.StringIDs128Offset + (0x80 * index), value.ToCharArray(), true);
                this.StringIDCache[index] = new StringID(value.ToString());
            }
        }

        public bool Contains(StringID stringID)
        {
            for (ushort i = 0; i < this.StringIDCache.Count; i = (ushort)(i + 1))
            {
                if (this.StringIDCache[i].ToString() == stringID.ToString())
                    return true;
            }
            return false;
        }
    }
}

